package com.haierp.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.model.InventoryAssistExpress;
import com.haierp.model.purchase.PurchaseAssistTime;
import com.haierp.service.IInventoryAssistExpressService;
import com.haierp.util.DateUtil;
import com.wangzhixuan.commons.base.BaseController;

/**
 *
 * InventoryAssistExpress 控制层
 *
 */
@Controller
@RequestMapping("/assistExpress")
public class InventoryAssistExpressController extends BaseController{

	@Autowired
	private IInventoryAssistExpressService inventoryAssistExpressService;

	@RequestMapping("/editAssistExpress")
	public String editAssistExpress(InventoryAssistExpress inventoryAssistExpress) {
		if(inventoryAssistExpress.getId()!=null) {
			inventoryAssistExpress.setGmtModify(new Date());
			inventoryAssistExpressService.updateSelectiveById(inventoryAssistExpress);
		} else {
			inventoryAssistExpress.setGmtCreate(new Date());
			inventoryAssistExpress.setGmtModify(new Date());
			inventoryAssistExpressService.insert(inventoryAssistExpress);
		}
		return "redirect:./queryassistExpress";
	}

	@RequestMapping("/queryassistExpress")
	public String queryassistExpress(HttpServletRequest request) {
		EntityWrapper<InventoryAssistExpress> entityWrapper = new EntityWrapper<>();
		entityWrapper.orderBy("gmt_create", false);
		List<InventoryAssistExpress> assistExpresstList = inventoryAssistExpressService.selectList(entityWrapper);
		Map<String,InventoryAssistExpress> expressMap = new HashMap<String,InventoryAssistExpress>();
		for (InventoryAssistExpress inventoryAssistExpress : assistExpresstList) {
			inventoryAssistExpress.setDimensionDate(DateUtil.formatSimpleDate(inventoryAssistExpress.getGmtCreate()));
			if(expressMap.containsKey(inventoryAssistExpress.getDimensionDate())) {
				InventoryAssistExpress inventoryAssistExpressNew = expressMap.get(inventoryAssistExpress.getDimensionDate());
				inventoryAssistExpressNew.setCount(inventoryAssistExpressNew.getCount()+1);
			} else {
				InventoryAssistExpress inventoryAssistExpressNew = new InventoryAssistExpress();
				inventoryAssistExpressNew.setCount(1L);
				expressMap.put(inventoryAssistExpress.getDimensionDate(), inventoryAssistExpressNew);
			}
		}
		request.setAttribute("expressMap", expressMap);
		request.setAttribute("assistExpresstList", assistExpresstList);
		return "kpi/assistExpress";
	}
}